import Common from '../../../actions/common';
import {getAppPageConfig} from 'src/hrpub/common/utils/utils';
export default class FormEdit extends Common {
    constructor(child, comp) {
        super();
        this.comp = comp;
    }

    // 表单编辑前
    formBeforeEdit = async (platFormProps, formId, key, value, data) => {

        const {props} = this.comp;
        const {emp, dispatch, form, meta} = props;
        
        let formData = form.getAllFormValue(formId);

        try {
            let res = await dispatch({
                type: 'emp/formEditBefore',
                payload: {
                    postData: {
                        key: key,
                        pk_org: emp.orgValue.refpk,
                        model: formData
                    }
                    
                }
            });

            let template = meta.getMeta();

            let formList = this.deepCopy(template['formrelation'][formId]);
            formList.push(formId);
            if(res.data && res.data.refParam) {
                formList.map((area) => {
                    template[area].items.map((item) => {
                        if(item.attrcode === key) {
                            if (!item.queryCondition) item.queryCondition = {}
                            Object.assign(item.queryCondition, {
                                pk_org: emp.orgValue.refpk
                            }, res.data.refParam);
                        }
                    });
                });
            }

            await new Promise((resolve, reject) => {
                meta.setMeta(template, resolve);
            });
        }
        catch(e) {
        }
        return true;
    }

    // 表单编辑后
    formAfterEdit = (platFormProps, formId, key, value, preVal) => {
        const {props} = this.comp;
        const {emp, dispatch, form, meta} = props;

        let formData = form.getAllFormValue(formId);
        let appConfig = getAppPageConfig();
        let postData = {
            app_code: appConfig.appcode,
            page_code: formId === 'card_batch'? '60092060batch': appConfig.pagecode,
            area_code: formId,
            key: key,
            model: formData,
            pk_org: emp.orgValue.refpk
        };

        dispatch({
            type: 'emp/formEditAfter',
            payload: {
                postData: postData
            }
        })
            .then((res) => {
                if(res.data) {
                    let template = meta.getMeta();

                    if(res.data.form && res.data.form.head) {
                        form.setAllFormValue({
                            [formId]: res.data.form.head.card
                        });
                    }
                    if('addreason_require' in res.data) {
                        form.setFormItemsRequired(formId, {
                            addreason: res.data.addreason_require
                        });
                    }
                    let disabled = {};
                    
                    if('addreason_disable' in res.data) {  
                        disabled['addreason'] =  !!res.data.addreason_disable;
                    }
                    if('isrelease' in res.data) {
                        disabled['isrelease'] =  !!res.data.isrelease;
                    }
                    if('isend' in res.data) {
                        disabled['isend'] =  !!res.data.isend;
                    }
                    if('contorgs' in res.data) {
                        template['contorgs'] = res.data.contorgs;
                    }

                    meta.setMeta(template, () => {
                        form.setFormItemsDisabled(formId, disabled);
                    });
                }
            });
    }
}